package com.mlethe.library.multi.api;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.mlethe.library.multi.api.entity.ImageObject;

import java.net.URL;

/**
 * 分享实体
 *
 * @author Mlethe
 */
public abstract class Share {
    /**
     * 标题
     */
    private String title;
    /**
     * 描述
     */
    private String description;
    /**
     * 缩略图
     * 微信不超过256k
     * 支付宝不超过32k
     */
    private ImageObject thumbImage;
    /**
     * 点击跳转url
     */
    private String targetUrl;

    /**
     * 获取标题
     */
    public String getTitle() {
        return title;
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public Share setTitle(String title) {
        this.title = title;
        return this;
    }

    /**
     * 获取描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 设置描述
     *
     * @param description 描述
     */
    public Share setDescription(String description) {
        this.description = description;
        return this;
    }

    /**
     * 获取缩略图
     */
    public ImageObject getThumbImage() {
        return thumbImage;
    }

    /**
     * 获取缩略图
     */
    public byte[] getThumbData(Context context) {
        if (thumbImage == null) {
            return null;
        }
        String imagePath = thumbImage.getImagePath();
        Bitmap bitmap = thumbImage.getBitmap();
        byte[] imageData = thumbImage.getImageData();
        int drawableId = thumbImage.getDrawableId();
        try {
            if (bitmap != null && !bitmap.isRecycled()) {
                imageData = Util.bitmapToByteArray(Util.bitmapFormat(bitmap));
            } else if (drawableId != 0) {
                Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawableId);
                imageData = Util.bitmapToByteArray(Util.bitmapFormat(bmp));
            } else if (!TextUtils.isEmpty(imagePath)) {
                URL url = Util.getUrl(imagePath);
                if (url != null) {
                    // 网络图片
                    Bitmap bmp = Util.url2bitmap(url);
                    imageData = Util.bitmapToByteArray(Util.bitmapFormat(bmp));
                } else {
                    // 本地图片
                    Bitmap bmp = BitmapFactory.decodeFile(imagePath);
                    imageData = Util.bitmapToByteArray(Util.bitmapFormat(bmp));
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
        }
        return imageData;
    }

    /**
     * 设置缩略图
     *
     * @param data 图片
     */
    public Share setThumbImage(ImageObject data) {
        this.thumbImage = data;
        return this;
    }

    /**
     * 获取跳转的url
     */
    public String getTargetUrl() {
        return targetUrl;
    }

    /**
     * 设置跳转的url
     *
     * @param url url
     */
    public Share setTargetUrl(String url) {
        this.targetUrl = url;
        return this;
    }

    /**
     * 是否有效
     *
     * @return true 有效，false 参数缺失
     */
    public boolean isValid() {
        return false;
    }

    /**
     * 释放内存
     */
    protected void release() {
        title = null;
        description = null;
        if (thumbImage != null) {
            thumbImage.release();
        }
        thumbImage = null;
    }
}
